/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.datatype.XMLGregorianCalendar;
import xml.jxb.ObjectFactory;
import xml.jxb.Project;
import xml.jxb.SimpleMode;
import xml.jxb.Unit;
import xml.jxb.Units;

/**
 *
 * @author abnormal
 */
@WebServlet(name = "Model", urlPatterns = {"/Model"})
public class Model extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InstantiationException, IllegalAccessException {
        if(request.getParameter("mode") == null){
            response.sendError(501, " mode undefined");
        }
        else if(request.getParameter("mode").equals("generate")){
            generateModels(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (InstantiationException ex) {
            Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
            response.sendError(500);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
            response.sendError(500);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (InstantiationException ex) {
            Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void generateModels(HttpServletRequest request, HttpServletResponse response) throws InstantiationException, IllegalAccessException {
        //plukker ut variabler
        int quantity = 0;
        float square;
        String projectName;
        String uValueStandard;
        String poststed;
        
        //lager prosjekt
        ObjectFactory factory = new ObjectFactory();
        Project proj = factory.createProject();
        proj.setDescription(factory.createDescription());
        proj.setUnits(factory.createUnits());
        proj.setProfiles(factory.createProfiles());
        proj.getDescription().setProjectAuthor((String)request.getSession().getAttribute("name"));
        proj.getDescription().setProjectName(request.getParameter("projectname"));
        proj.getDescription().setProjectCreationDate(XMLGregorianCalendarImpl.class.newInstance());
        for(int i= 0; i < quantity; i++){
            Unit u = factory.createUnit();
            SimpleMode sm = factory.createSimpleMode();
            
            u.setSimple(null);
            proj.getUnits().getUnit().add(u);
        }
    }
}
